import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedstate from 'vuex-persistedstate'
import router, {resetRouter} from "@/router";

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        configuration:{
            isTagsViews: true,
            isLogo: true,
            isTimeShow:true,
            isDynamicTitle: true
        },
        currentPathNames: [],
        tabList: [
            {
                path: '/home', menuName: '主页'
            }
        ]
    },
    mutations: {
        setPath(state) {
            state.currentPathNames = JSON.parse(localStorage.getItem("currentPathNames"))[1]=='主页'?[]: JSON.parse(localStorage.getItem("currentPathNames")).slice(1);
            console.log(state.currentPathNames+"");
        },
        selectMenu(state,val){
            if(val.menuName!='主页'){
                let result=state.tabList.some(item=>item.menuName===val.menuName);
                if(result){
                    return;
                }
                state.tabList.push(val);
            }
        },
        closeTab(state,val){
            let result=state.tabList.findIndex(item=>item.menuName===val.menuName);
            state.tabList.splice(result,1);
        },
        logout() {
            localStorage.removeItem("user");
            localStorage.removeItem("menuList");
            localStorage.removeItem("currentPathNames");
            store.replaceState({
                currentPathNames: [],
                tabList: [
                    {
                        path: '/home', menuName: '主页'
                    }
                ]});
            router.push("/login");
            resetRouter();
        }
    },
    plugins:[createPersistedstate({
        key:'tabList',
        reducer(val){
            return{
                tabList: val.tabList
            }
        }
    })]
})

export default store
